package org.zhs.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.zhs.es.test.mappingVo.User;

import java.io.IOException;

/**
 * @Author: zhs
 * @Date: 2021/6/24 11:06
 * @Description:
 *
 * 批量插入文档
 *
 */
public class ESTest_Document_Insert_Batch {
    public static void main(String[] args) throws IOException {
        //创建ES客户端
        RestHighLevelClient rsClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));


        //批量插入数据
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name","zhangsan","age",30,"sex","男"));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name","lisi","age",40,"sex","男"));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name","wangwu","age",50,"sex","男"));
        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name","zhangsan22","age",60,"sex","男"));
        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name","lisi22","age",30,"sex","女"));
        request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name","wangwu22","age",30,"sex","女"));


        BulkResponse response = rsClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook()); //花费的时间
        System.out.println(response.getItems()); //获得多个添加的返回


        //关闭客户端
        rsClient.close();
    }
}
